package com.etang.a2020b1006;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.etang.a2020b1006.fragment.BarActivity;

public class LoadingActivity extends AppCompatActivity {

    private ProgressBar pg_1;
    private TextView tv_loading_number;
    private int number = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading);
        pg_1 = (ProgressBar) findViewById(R.id.progressBar);
        tv_loading_number = (TextView) findViewById(R.id.tv_loading_text);
        handler.post(timeRunnable);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            pg_1.setProgress(number);
            switch (number) {
                case 100:
                    tv_loading_number.setText("正在加载串口配置......");
                    break;
                case 200:
                    tv_loading_number.setText("串口配置加载完成......");
                    break;
                case 300:
                    tv_loading_number.setText("正在加载界面配置......");
                    break;
                case 500:
                    tv_loading_number.setText("界面配置加载完成......");
                    break;
                case 600:
                    tv_loading_number.setText("正在初始化界面......");
                    break;
                case 800:
                    tv_loading_number.setText("界面初始化完成......");
                    break;
                case 900:
                    tv_loading_number.setText("正在进入系统......");
                    break;
            }
            handler.postDelayed(timeRunnable, 1);
        }
    };
    Runnable timeRunnable = new Runnable() {
        @Override
        public void run() {
            number = number + 5;
            Message msg = handler.obtainMessage();
            if (number > 1000) {
                handler.removeCallbacks(timeRunnable);
                startActivity(new Intent(getApplicationContext(), BarActivity.class));
                finish();
            } else {
                handler.sendMessage(msg);
            }
        }
    };
}